home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / GAMES / C014.ZIP / LARN_SRC.ZIP / DIAG.C < prev    next >
C/C++ Source or Header  |  1993-11-17  |  14KB  |  426 lines

  1. /* diag.c */
  2. #ifdef VMS
  3. # include <types.h>
  4. # include <stat.h>
  5. #else
  6. # include <sys/types.h>
  7. # include <sys/stat.h>
  8. #endif VMS
  9.  
  10. #ifndef MSDOS
  11. # ifndef VMS
  12. #  include <sys/times.h>
  13.   static struct tms cputime;
  14. # endif VMS
  15. #endif MSDOS
  16.  
  17. #include "header.h"
  18. #include "larndefs.h"
  19. #include "monsters.h"
  20. #include "objects.h"
  21. #include "player.h"
  22.  
  23. extern long int initialtime;
  24. extern int rmst,maxitm,lasttime;
  25. extern char nosignal;
  26.  
  27. /*
  28.     ***************************
  29.     DIAG -- dungeon diagnostics
  30.     ***************************
  31.  
  32.     subroutine to print out data for debugging
  33.  */
  34. #ifdef EXTRA
  35. static int rndcount[16];
  36. diag()
  37.     {
  38.     register int i,j;
  39.     int hit,dam;
  40.     cursors();  lwclose();
  41.     if (lcreat(diagfile) < 0)   /*  open the diagnostic file    */
  42.         {
  43.         lcreat((char*)0); lprcat("\ndiagnostic failure\n"); return(-1);
  44.         }
  45.  
  46.     write(1,"\nDiagnosing . . .\n",18);
  47.     lprcat("\n\nBeginning of DIAG diagnostics ----------\n");
  48.  
  49. /*  for the character attributes    */
  50.  
  51.     lprintf("\n\nPlayer attributes:\n\nHit points: %2d(%2d)",(long)c[HP],(long)c[HPMAX]);
  52.     lprintf("\ngold: %d  Experience: %d  Character level: %d  Level in caverns: %d",
  53.         (long)c[GOLD],(long)c[EXPERIENCE],(long)c[LEVEL],(long)level);
  54.     lprintf("\nTotal types of monsters: %d",(long)MAXMONST+8);
  55.  
  56.     lprcat("\f\nHere's the dungeon:\n\n");
  57.  
  58.     i=level;
  59.     for (j=0; j<MAXLEVEL+MAXVLEVEL; j++)
  60.         {
  61.         newcavelevel(j);
  62.         lprintf("\nMaze for level %s:\n",levelname[level]);
  63.         diagdrawscreen();
  64.         }
  65.     newcavelevel(i);
  66.  
  67.     lprcat("\f\nNow for the monster data:\n\n");
  68.     lprcat("   Monster Name      LEV  AC   DAM  ATT  DEF    GOLD   HP     EXP   \n");
  69.     lprcat("--------------------------------------------------------------------------\n");
  70.     for (i=0; i<=MAXMONST+8; i++)
  71.         {
  72.         lprintf("%19s  %2d  %3d ",monster[i].name,(long)monster[i].level,(long)monster[i].armorclass);
  73.         lprintf(" %3d  %3d  %3d  ",(long)monster[i].damage,(long)monster[i].attack,(long)monster[i].defense);
  74.         lprintf("%6d  %3d   %6d\n",(long)monster[i].gold,(long)monster[i].hitpoints,(long)monster[i].experience);
  75.         }
  76.  
  77.     lprcat("\n\nHere's a Table for the to hit percentages\n");
  78.     lprcat("\n     We will be assuming that players level = 2 * monster level");
  79.     lprcat("\n     and that the players dexterity and strength are 16.");
  80.     lprcat("\n    to hit: if (rnd(22) < (2[monst AC] + your level + dex + WC/8 -1)/2) then hit");
  81.     lprcat("\n    damage = rund(8) + WC/2 + STR - c[HARDGAME] - 4");
  82.     lprcat("\n    to hit:  if rnd(22) < to hit  then player hits\n");
  83.     lprcat("\n    Each entry is as follows:  to hit / damage / number hits to kill\n");
  84.     lprcat("\n          monster     WC = 4         WC = 20        WC = 40");
  85.     lprcat("\n---------------------------------------------------------------");
  86.     for (i=0; i<=MAXMONST+8; i++)
  87.         {
  88.         hit = 2*monster[i].armorclass+2*monster[i].level+16;
  89.         dam = 16 - c[HARDGAME];
  90.         lprintf("\n%20s   %2d/%2d/%2d       %2d/%2d/%2d       %2d/%2d/%2d",
  91.                     monster[i].name,
  92.                     (long)(hit/2),(long)max(0,dam+2),(long)(monster[i].hitpoints/(dam+2)+1),
  93.                     (long)((hit+2)/2),(long)max(0,dam+10),(long)(monster[i].hitpoints/(dam+10)+1),
  94.                     (long)((hit+5)/2),(long)max(0,dam+20),(long)(monster[i].hitpoints/(dam+20)+1));
  95.         }
  96.  
  97.     lprcat("\n\nHere's the list of available potions:\n\n");
  98.     for (i=0; i<MAXPOTION; i++) lprintf("%20s\n",&potionname[i][1]);
  99.     lprcat("\n\nHere's the list of available scrolls:\n\n");
  100.     for (i=0; i<MAXSCROLL; i++) lprintf("%20s\n",&scrollname[i][1]);
  101.     lprcat("\n\nHere's the spell list:\n\n");
  102.     lprcat("spell          name           description\n");
  103.     lprcat("-------------------------------------------------------------------------------------------\n\n");
  104.     for (j=0; j<SPNUM; j++)
  105.         {
  106.         lprc(' ');  lprcat(spelcode[j]);
  107.         lprintf(" %21s  %s\n",spelname[j],speldescript[j]); 
  108.         }
  109.  
  110.     lprcat("\n\nFor the c[] array:\n");
  111.     for (j=0; j<100; j+=10)
  112.         {
  113.         lprintf("\nc[%2d] = ",(long)j); for (i=0; i<9; i++) lprintf("%5d ",(long)c[i+j]);
  114.         }
  115.  
  116.     lprcat("\n\nTest of random number generator ----------------");
  117.     lprcat("\n    for 25,000 calls divided into 16 slots\n\n");
  118.  
  119.     for (i=0; i<16; i++)  rndcount[i]=0;
  120.     for (i=0; i<25000; i++) rndcount[rund(16)]++;
  121.     for (i=0; i<16; i++)  { lprintf("  %5d",(long)rndcount[i]); if (i==7) lprc('\n'); }
  122.  
  123.     lprcat("\n\n");         lwclose();
  124.     lcreat((char*)0);       lprcat("Done Diagnosing . . .");
  125.     return(0);
  126.     }
  127. /*
  128.     subroutine to count the number of occurrences of an object
  129.  */
  130. dcount(l)
  131.     int l;
  132.     {
  133.     register int i,j,p;
  134.     int k;
  135.     k=0;
  136.     for (i=0; i<MAXX; i++)
  137.         for (j=0; j<MAXY; j++)
  138.             for (p=0; p<MAXLEVEL; p++)
  139.                 if (cell[(long) p*MAXX*MAXY+i*MAXY+j].item == l) k++;
  140.     return(k);
  141.     }
  142.  
  143. /*
  144.     subroutine to draw the whole screen as the player knows it
  145.  */
  146. diagdrawscreen()
  147.     {
  148.     register int i,j,k;
  149.  
  150.     for (i=0; i<MAXY; i++)
  151.  
  152. /*  for the east west walls of this line    */
  153.         {
  154.         for (j=0; j<MAXX; j++)  if (k=mitem[j][i]) lprc(monstnamelist[k]); else
  155.                                 lprc(objnamelist[item[j][i]]);
  156.         lprc('\n');
  157.         }
  158.     }
  159. #endif
  160.  
  161. /*
  162.     to save the game in a file
  163.  */
  164. static long int zzz=0;
  165. savegame(fname)
  166.     char *fname;
  167.     {
  168.     extern char lastmonst[], course[];
  169.     register int i,k;
  170.     register struct sphere *sp;
  171.     struct stat statbuf;
  172. # ifdef MSDOS
  173.     struct  cel buf[MAXX];
  174.     RAMBLOCK    *rp;
  175.     DISKBLOCK   *dp;
  176. # endif
  177.  
  178.     nosignal=1;  lflush();  savelevel();
  179.     ointerest();
  180.     if (lcreat(fname) < 0)
  181.         {
  182.         lcreat((char*)0); lprintf("\nCan't open file <%s> to save game\n",fname);
  183.         nosignal=0;  return(-1);
  184.         }
  185.  
  186.     set_score_output();
  187.  
  188. # ifdef MSDOS
  189.     lwrite((char*)beenhere,MAXLEVEL+MAXVLEVEL);
  190.  
  191.     /* Some levels may be out on disk, some are in memory.
  192.      */
  193.     for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
  194.         if (beenhere[k]) {
  195.  
  196.             /* Is the level in memory already ?
  197.              */
  198.             for (rp = ramblks; rp; rp = rp->next)
  199.                 if (rp->level == k)
  200.                     break;
  201.             if (rp != NULL) {
  202.                 lwrite((char *) &rp->gtime, sizeof (long));
  203.                 warn(" %d", k);
  204. # ifdef ndef
  205.                 warn("From memory\n", k);
  206. # endif
  207.                 lwrite((char *) rp->cell, sizeof rp->cell);
  208.                 continue;
  209.             }
  210.  
  211.             /* Is it on disk ?
  212.              */
  213.             for (dp = diskblks; dp; dp = dp->next)
  214.                 if (dp->level == k)
  215.                     break;
  216.  
  217.             if (dp == NULL) {
  218.                 levelinfo();
  219.                 error("Level %d is neither in memory nor on disk\n", k);
  220.             }
  221.  
  222.             /* Copy the contents of the SWAPFILE */
  223.             lwrite((char *) &dp->gtime, sizeof (long));
  224. # ifdef ndef
  225.             warn("From swap file...\n", k);
  226. # endif
  227.             warn(" %ds", k);
  228.             if (lseek(swapfd, dp->fpos, 0) < 0) {
  229.                 warn("Can't seek to %ld\n", dp->fpos);
  230.                 return -1;
  231.             }
  232.             for (i = 0; i < MAXY; i++) {
  233. # ifdef ndef
  234.                 warn("Copying swap file...\n");
  235. # endif
  236.                 if (vread(swapfd, (char *) buf, sizeof buf) !=
  237.                     sizeof buf) {
  238.                     warn("Swap file short!\n");
  239.                     return -1;
  240.                 }
  241.                 lwrite((char *) buf, sizeof buf);
  242.             }
  243.         }
  244. # else
  245.     lwrite((char*)beenhere,MAXLEVEL+MAXVLEVEL);
  246.     for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
  247.         if (beenhere[k])
  248.             lwrite((char*)&cell[(long) k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
  249. # endif
  250. #ifndef VMS
  251. # ifndef MSDOS
  252.     times(&cputime);    /* get cpu time */
  253.     c[CPUTIME] += (cputime.tms_utime+cputime.tms_stime)/60;
  254. # endif MSDOS
  255. #endif VMS
  256.     lwrite((char*)&c[0],100*sizeof(long));
  257.     lprint((long)gtime);        lprc(level);
  258.     lprc(playerx);      lprc(playery);
  259.     lwrite((char*)iven,26); lwrite((char*)ivenarg,26*sizeof(short));
  260.     for (k=0; k<MAXSCROLL; k++)  lprc(scrollname[k][0]);
  261.     for (k=0; k<MAXPOTION; k++)  lprc(potionname[k][0]);
  262.     lwrite((char*)spelknow,SPNUM);       lprc(wizard);
  263.     lprc(rmst);     /*  random monster generation counter */
  264.     for (i=0; i<90; i++)    lprc(itm[i].qty);
  265.     lwrite((char*)course,25);           lprc(cheat);        lprc(VERSION);
  266.     for (i=0; i<MAXMONST; i++) lprc(monster[i].genocided); /* genocide info */
  267.     for (sp=spheres; sp; sp=sp->p)
  268.         lwrite((char*)sp,sizeof(struct sphere));    /* save spheres of annihilation */
  269.     time(&zzz);         lprint((long)(zzz-initialtime));
  270.     lwrite((char*)&zzz,sizeof(long));
  271. # ifndef MSDOS
  272.     if (fstat(lfd,&statbuf)< 0) lprint(0L);
  273.     else lprint((long)statbuf.st_ino); /* inode # */
  274. # endif
  275.  
  276.     lwclose();  lastmonst[0] = 0;
  277. #ifndef VT100
  278.     setscroll();
  279. #endif VT100
  280.     lcreat((char*)0);  nosignal=0;
  281.     return(0);
  282.     }
  283.  
  284. restoregame(fname)
  285.     char *fname;
  286.     {
  287.     register int i,k;
  288.     register struct sphere *sp,*sp2;
  289.     struct stat filetimes;
  290. # ifdef MSDOS
  291.     RAMBLOCK    *rp, *getramblk();
  292. # endif
  293.  
  294.     cursors(); lprcat("\nRestoring . . .");  lflush();
  295.     if (lopen(fname) <= 0)
  296.         {
  297.         lcreat((char*)0); lprintf("\nCan't open file <%s>to restore game\n",fname);
  298.         nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-265); return;
  299.         }
  300.     lrfill((char*)beenhere,MAXLEVEL+MAXVLEVEL);
  301.  
  302. # ifdef MSDOS
  303.     /* As levels get read in from the save file, store them away
  304.      * by calling putsavelevel.
  305.      */
  306.     for (k = 0; k < MAXLEVEL + MAXVLEVEL; k++)
  307.         if (beenhere[k]) {
  308.             rp = getramblk(k);
  309.             lrfill((char *) &rp->gtime, sizeof (long));
  310.             lrfill((char *) rp->cell, sizeof rp->cell);
  311.             rp->level = k;
  312.         }
  313. # else
  314.     for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
  315.         if (beenhere[k])
  316.             lrfill((char*)&cell[(long) k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
  317. # endif
  318.  
  319.     lrfill((char*)&c[0],100*sizeof(long));  gtime = lrint();
  320.     level = c[CAVELEVEL] = lgetc();
  321.     playerx = lgetc();      playery = lgetc();
  322.     lrfill((char*)iven,26);     lrfill((char*)ivenarg,26*sizeof(short));
  323.     for (k=0; k<MAXSCROLL; k++)  scrollname[k][0] = lgetc();
  324.     for (k=0; k<MAXPOTION; k++)  potionname[k][0] = lgetc();
  325.     lrfill((char*)spelknow,SPNUM);      wizard = lgetc();
  326.     rmst = lgetc();         /*  random monster creation flag */
  327.  
  328.     for (i=0; i<90; i++)    itm[i].qty = lgetc();
  329.     lrfill((char*)course,25);           cheat = lgetc();
  330.     if (VERSION != lgetc())     /*  version number  */
  331.         {
  332.         cheat=1;
  333.         lprcat("Sorry, But your save file is for an older version of larn\n");
  334.         nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-266); return;
  335.         }
  336.  
  337.     for (i=0; i<MAXMONST; i++) monster[i].genocided=lgetc(); /* genocide info */
  338.     for (sp=0,i=0; i<c[SPHCAST]; i++)
  339.         {
  340.         sp2 = sp;
  341.         sp = (struct sphere *)malloc(sizeof(struct sphere));
  342.         if (sp==0) { write(2,"Can't malloc() for sphere space\n",32); break; }
  343.         lrfill((char*)sp,sizeof(struct sphere));    /* get spheres of annihilation */
  344.         sp->p=0;    /* null out pointer */
  345.         if (i==0) spheres=sp;   /* beginning of list */
  346.             else sp2->p = sp;
  347.         }
  348.  
  349.     time(&zzz);
  350.     initialtime = zzz-lrint();
  351.     fstat(fd,&filetimes);   /*  get the creation and modification time of file  */
  352.     lrfill((char*)&zzz,sizeof(long));   zzz += 6;
  353.     if (filetimes.st_ctime > zzz) fsorry(); /*  file create time    */
  354.     else if (filetimes.st_mtime > zzz) fsorry(); /* file modify time    */
  355.     if (c[HP]<0) { died(284); return; } /* died a post mortem death */
  356.  
  357.     oldx = oldy = 0;
  358. #ifndef VMS
  359. # ifndef MSDOS
  360.     i = lrint();  /* inode # */
  361.     if (i && (filetimes.st_ino!=i)) fsorry();
  362. # endif MSDOS
  363. #endif VMS
  364.     lrclose();
  365.     if (strcmp(fname,ckpfile) == 0)
  366.         {
  367.         if (lappend(fname) < 0) fcheat();  else { lprc(' '); lwclose(); }
  368.         lcreat((char*)0);
  369.         }
  370.     else if (unlink(fname) < 0) fcheat(); /* can't unlink save file */
  371. /*  for the greedy cheater checker  */
  372.     for (k=0; k<6; k++) if (c[k]>99) greedy();
  373.     if (c[HPMAX]>999 || c[SPELLMAX]>125) greedy();
  374.     if (c[LEVEL]==25 && c[EXPERIENCE]>skill[24]) /* if patch up lev 25 player */
  375.         {
  376.         long tmp;
  377.         tmp = c[EXPERIENCE]-skill[24]; /* amount to go up */
  378.         c[EXPERIENCE] = skill[24];
  379.         raiseexperience((long)tmp);
  380.         }
  381.     getlevel();  lasttime=gtime;
  382.     }
  383.  
  384. /*
  385.     subroutine to not allow greedy cheaters
  386.  */
  387. static greedy()
  388.     {
  389. #if WIZID
  390.     if (wizard) return;
  391. #endif
  392.  
  393.     lprcat("\n\nI am so sorry, but your character is a little TOO good!  Since this\n");
  394.     lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
  395.     lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
  396.     lprcat("to continue.\n"); nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-267); return;
  397.     }
  398.  
  399. /*
  400.     subroutine to not allow altered save files and terminate the attempted
  401.     restart
  402.  */
  403. static fsorry()
  404.     {
  405.     lprcat("\nSorry, but your savefile has been altered.\n");
  406.     lprcat("However, seeing as I am a good sport, I will let you play.\n");
  407.     lprcat("Be advised though, you won't be placed on the normal scoreboard.");
  408.     cheat = 1;  nap(4000);
  409.     }
  410.  
  411. /*
  412.     subroutine to not allow game if save file can't be deleted
  413.  */
  414. static fcheat()
  415.     {
  416. #if WIZID
  417.     if (wizard) return;
  418. #endif
  419.  
  420.     lprcat("\nSorry, but your savefile can't be deleted.  This can only mean\n");
  421.     lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
  422.     lprcat("is in.  Since this is unfair to the rest of the larn community, I\n");
  423.     lprcat("cannot let you play this game.\n");
  424.     nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-268); return;
  425.     }
  426.